package com.cq.hd.merchant.controller;

import com.cq.hd.common.annotation.MerchantLoginUser;
import com.cq.hd.common.entity.MerchantAppLoginUser;
import com.cq.hd.common.enums.biz.WithdrawalTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.member.api.TbWithdrawalBillApi;
import com.cq.hd.member.api.dto.CheckBillConfirmDto;
import com.cq.hd.member.api.dto.WithdrawalApplyDto;
import com.cq.hd.member.api.dto.WithdrawalApplyPageDto;
import com.cq.hd.member.api.dto.WithdrawalApplyV2Dto;
import com.cq.hd.member.api.vo.SubsidyNoEnoughVo;
import com.cq.hd.member.api.vo.WithdrawalApplyPageVo;
import com.cq.hd.member.api.vo.WithdrawalCheckBillVo;
import com.cq.hd.merchant.biz.MerchantBiz;
import com.cq.hd.merchant.vo.BalanceAmtWithdrawalPreVo;
import com.cq.hd.merchant.vo.SubsidyMonthSettlePreVo;
import com.cq.hd.merchant.vo.WithdrawalPreVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/withdrawal")
@Api(tags = "提现相关接口")
public class WithdrawalController {

    @Autowired
    private MerchantBiz merchantBiz;

    @Autowired
    private TbWithdrawalBillApi withdrawalBillApi;

    @ApiOperation(value = "预提现")
    @ApiResponse(response = WithdrawalPreVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pre")
    public Result<WithdrawalPreVo> pre(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        return ResultGenerator.success(merchantBiz.preWithdrawal(merchantAppLoginUser));
    }

    @ApiOperation(value = "申请商户本金提现")
    @ApiImplicitParam(name = "withdrawalApplyDto", value = "实体", paramType = "body", dataType = "WithdrawalApplyDto")
    @PostMapping("/amtApply")
    public Result<Boolean> amtApply(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser, @RequestBody WithdrawalApplyDto withdrawalApplyDto) {
        Long merchantId = merchantAppLoginUser.getMerchantId();
        withdrawalApplyDto.setUserId(merchantId);
        withdrawalApplyDto.setWithdrawalType(WithdrawalTypeEnum.MERCHANT_AMT.getValue());
        withdrawalApplyDto.setFinancialAccountType(merchantBiz.getFinancialAccountType(merchantId));
        return withdrawalBillApi.apply(withdrawalApplyDto);
    }

    @ApiOperation(value = "商户本金提现预览页")
    @ApiResponse(response = BalanceAmtWithdrawalPreVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/balanceAmtPre")
    public Result<BalanceAmtWithdrawalPreVo> balanceAmtWithdrawalPre(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        return ResultGenerator.success(merchantBiz.balanceAmtWithdrawalPre(merchantAppLoginUser.getMerchantId()));
    }


    @ApiOperation(value = "商户本金提现")
    @ApiImplicitParam(name = "withdrawalApplyV2Dto", value = "实体", paramType = "body", dataType = "withdrawalApplyV2Dto")
    @PostMapping("/balanceAmtApply")
    public Result<Boolean> balanceAmtApply(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser,
                                           @RequestBody WithdrawalApplyV2Dto withdrawalApplyV2Dto) {
        Long merchantId = merchantAppLoginUser.getMerchantId();
        withdrawalApplyV2Dto.setUserId(merchantId);
        withdrawalApplyV2Dto.setWithdrawalType(WithdrawalTypeEnum.MERCHANT_AMT.getValue());
        withdrawalApplyV2Dto.setFinancialAccountType(merchantBiz.getFinancialAccountType(merchantId));
        return withdrawalBillApi.applyV2(withdrawalApplyV2Dto);
    }

    @ApiOperation(value = "商户补贴结算单列表")
    @GetMapping("/subsidyMonthSettleList")
    public Result<SubsidyMonthSettlePreVo> subsidyMonthSettleList(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        int financialAccountType = merchantBiz.getFinancialAccountType(merchantAppLoginUser.getMerchantId());
        SubsidyMonthSettlePreVo subsidyMonthSettlePreVo = merchantBiz.subsidyMonthSettleList(merchantAppLoginUser.getMerchantId(), financialAccountType);
        return ResultGenerator.success(subsidyMonthSettlePreVo);
    }

    @ApiOperation(value = "申请商户补贴提现v2")
    @ApiImplicitParam(name = "WithdrawalApplyV2Dto", value = "实体", paramType = "body", dataType = "withdrawalApplyV2Dto")
    @PostMapping("/subsidyApplyV2")
    public Result<Boolean> subsidyApplyV2(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser, @RequestBody WithdrawalApplyV2Dto withdrawalApplyV2Dto) {
        Long merchantId = merchantAppLoginUser.getMerchantId();
        withdrawalApplyV2Dto.setUserId(merchantId);
        withdrawalApplyV2Dto.setWithdrawalType(WithdrawalTypeEnum.MERCHANT_SUBSIDY.getValue());
        withdrawalApplyV2Dto.setFinancialAccountType(merchantBiz.getFinancialAccountType(merchantId));
        return withdrawalBillApi.applyV2(withdrawalApplyV2Dto);
    }

    @ApiOperation(value = "申请商户补贴提现v1")
    @ApiImplicitParam(name = "withdrawalApplyDto", value = "实体", paramType = "body", dataType = "WithdrawalApplyDto")
    @PostMapping("/subsidyApply")
    public Result<Boolean> subsidyApply(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser, @RequestBody WithdrawalApplyDto withdrawalApplyDto) {
        Long merchantId = merchantAppLoginUser.getMerchantId();
        withdrawalApplyDto.setUserId(merchantId);
        withdrawalApplyDto.setWithdrawalType(WithdrawalTypeEnum.MERCHANT_SUBSIDY.getValue());
        withdrawalApplyDto.setFinancialAccountType(merchantBiz.getFinancialAccountType(merchantId));
        return withdrawalBillApi.apply(withdrawalApplyDto);
    }

    @ApiOperation(value = "提现记录")
    @GetMapping("/page")
    public Result<Page<WithdrawalApplyPageVo>> page(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser, WithdrawalApplyPageDto withdrawalApplyPageDto) {
        withdrawalApplyPageDto.setUserId(merchantAppLoginUser.getMerchantId());
        if (withdrawalApplyPageDto.getWithdrawalTypes() == null) {
            withdrawalApplyPageDto.setWithdrawalTypes(Arrays.asList(WithdrawalTypeEnum.MERCHANT_AMT.getValue(), WithdrawalTypeEnum.MERCHANT_SUBSIDY.getValue()));
        }
        return withdrawalBillApi.pageMerchantWithdrawalBill(withdrawalApplyPageDto);
    }

    @ApiOperation(value = "补贴金不足的商家列表")
    @GetMapping("/getSubsidyNoEnough")
    public Result<SubsidyNoEnoughVo> getSubsidyNoEnough(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        return withdrawalBillApi.getSubsidyNoEnough(merchantAppLoginUser.getMerchantId());
    }

    @ApiOperation(value = "提现核算单详情")
    @GetMapping("/checkBillInfo")
    public Result<WithdrawalCheckBillVo> checkBillInfo(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser,
                                                       @RequestParam("checkBillId") Long checkBillId) {
        return withdrawalBillApi.checkBillInfo(checkBillId);
    }

    @ApiOperation(value = "提现核算单确认")
    @PostMapping("/checkBillConfirm")
    public Result<Boolean> checkBillConfirm(@ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser,
                                            @RequestBody CheckBillConfirmDto checkBillConfirmDto) {
        return withdrawalBillApi.checkBillConfirm(checkBillConfirmDto);
    }

}
